package com.easy.util;

import cn.hutool.crypto.digest.DigestUtil;

//加密工具类
public class PasswordUtil {
    private final static char[] arr={'a','b','c','d','e','f','0','1','2','3','4','5','6','7','8','9'};
    private final static int salt_index=5;
    private final static int count_index=15;
    //获取加密密码
    public static String getEncrypyion(String password){
        //随机盐值
        int index_salt=(int)(Math.random()*arr.length);
        char salt=arr[index_salt];
        //随机加密次数
        int index_count=(int)(Math.random()*arr.length);
        char count=arr[index_count];
        //随即加密
        String encrypyionPassword=password+salt;
        for (int i = 0; i < count; i++) {
            encrypyionPassword=DigestUtil.md5Hex(encrypyionPassword);
        }
        StringBuilder strb=new StringBuilder(encrypyionPassword);
        strb.insert(salt_index,salt);
        strb.insert(count_index,count);
        //返回
        return strb.toString();

    }

    //验证密码
    public static boolean checkPassword(String password,String encrypyionPassword){
        //获取盐值
        char salt=encrypyionPassword.charAt(salt_index);
        //获取加密次数
        char count=encrypyionPassword.charAt(count_index);
        //加密
        String decode=password+salt;
        for (int i = 0; i < count; i++) {
            decode=DigestUtil.md5Hex(decode);
        }
        StringBuilder strb=new StringBuilder(decode);
        strb.insert(salt_index,salt);
        strb.insert(count_index,count);
        return strb.toString().equals(encrypyionPassword);
    }

//    public static void main(String[] args) {
//        System.out.println(checkPassword("123456","5321e7dde43dc541706f2e851c59167758"));
//    }
}









































